<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2021/12/22
 * Time: 2:06 下午
 */

namespace app\api\controller\v29;

use app\api\controller\v2\Auth;
use app\common\cachemodel\live\LiveBroadcastPreview;
use app\common\cachemodel\live\LiveSubscribe;

/**
 * Class Live
 * @package app\api\controller\v29
 */
class LiveUser extends Auth
{
    /**
     * 直播预约或取消预约
     */
    public function subscribeLive()
    {
        $id = input('live_broadcast_id/d', 0);
        $type = input('type/d', 0);
        $time = time();
        $broadcastData = (new LiveBroadcastPreview())->where(['id' => $id])->field('live_id')->find();
        if (!$broadcastData)
        {
            return show(config('code.error'), '预约失败');
        }
        $LiveSubscribeModel = new LiveSubscribe();
        $sub = $LiveSubscribeModel->getUserSubscribeId($this->user['id'], $id);
        $msg = '预约';
        if ($type === 1)
        {
            $data = ['status' => 1, 'update_time' => $time];
        } else
        {
            $data = ['status' => 2, 'update_time' => $time];
            $msg = '取消预约';
        }
        //预约修改
        if ($sub)
        {
            $res = $LiveSubscribeModel->updateData(['id' => $sub['id'], 'uid' => $this->user['id'], 'borId' => $id], $data);
        } else//添加预约
        {
            $res = $LiveSubscribeModel->saveData(['uid' => $this->user['id'], 'broadcast_id' => $id, 'status' => $data['status'], 'live_id' => $broadcastData['live_id'], 'create_time' => $time]);
        }
        if ($res === true)
        {
            return show(config('code.success'), $msg . '成功');
        }
        return show(config('code.error'), $msg . '失败');
    }
}
